package com.project.common.core.utils.push;


import com.project.common.core.utils.push.android.*;
import com.project.common.core.utils.push.ios.*;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


public class PushUtils {

    private static PushClient client = new PushClient();

    public static void sendAndroidUnicast(String appkey, String appMasterSecret, String ticker, String title, String text, String deviceToken,Integer type) throws Exception {
        AndroidUnicast unicast = new AndroidUnicast(appkey, appMasterSecret);
        unicast.setDeviceToken(deviceToken);
        unicast.setTicker(ticker);
        unicast.setTitle(title);
        unicast.setText(text);
        Map<String,Object> map = new HashMap<>();
        map.put("type",type);
        unicast.goCustomAfterOpen(com.alibaba.fastjson.JSONObject.toJSONString(map));
        unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        unicast.setProductionMode();
        client.send(unicast);
    }

    /**
     *
     * @param appkey
     * @param appMasterSecret
     * @param deviceToken
     * @param title
     * @param subTitle
     * @param body
     * @param badge
     * @throws Exception
     */
    public static void sendIOSUnicast(String appkey, String appMasterSecret, String deviceToken, String title, String subTitle, String body,Integer badge) throws Exception {
        IOSUnicast unicast = new IOSUnicast(appkey, appMasterSecret);
        unicast.setDeviceToken(deviceToken);
        unicast.setAlert(title, subTitle, body);
        unicast.setBadge(badge);
        unicast.setSound("default");
//        unicast.setTestMode();
        unicast.setProductionMode();
//        unicast.setCustomizedField("test", "helloworld");
//        unicast.setDescription("测试");
        unicast.setCustomizedField("type","4");
        client.send(unicast);
    }

    public static void sendAndroidBroadcast(String appkey, String appMasterSecret,String ticker, String text) throws Exception {
        AndroidBroadcast broadcast = new AndroidBroadcast(appkey, appMasterSecret);
        broadcast.setTicker(ticker);
        broadcast.setText(text);
        broadcast.setTitle("");
        broadcast.setProductionMode();
//        broadcast.setTestMode();
        //type 推送系统消息 跳转到系统消息列表
        Map<String,Object> map = new HashMap<>();
        map.put("type",5);
        broadcast.goCustomAfterOpen(com.alibaba.fastjson.JSONObject.toJSONString(map));
        broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        client.send(broadcast);
    }


    public static void sendIOSBroadcast(String appkey, String appMasterSecret,String title,String body) throws Exception {
        IOSBroadcast broadcast = new IOSBroadcast(appkey, appMasterSecret);
        broadcast.setAlert(title, "", body);
        broadcast.setBadge(1);
        broadcast.setSound("default");
        broadcast.setProductionMode();
//        broadcast.setTestMode();
        //type 推送系统消息 跳转到系统消息列表
        broadcast.setCustomizedField("type","5");
        client.send(broadcast);
    }

    public void sendAndroidGroupcast(String appkey, String appMasterSecret) throws Exception {
        AndroidGroupcast groupcast = new AndroidGroupcast(appkey, appMasterSecret);
        /*  TODO
         *  Construct the filter condition:
         *  "where":
         *	{
         *		"and":
         *		[
         *			{"tag":"test"},
         *			{"tag":"Test"}
         *		]
         *	}
         */
        JSONObject filterJson = new JSONObject();
        JSONObject whereJson = new JSONObject();
        JSONArray tagArray = new JSONArray();
        JSONObject testTag = new JSONObject();
        JSONObject TestTag = new JSONObject();
        testTag.put("tag", "test");
        TestTag.put("tag", "Test");
        tagArray.put(testTag);
        tagArray.put(TestTag);
        whereJson.put("and", tagArray);
        filterJson.put("where", whereJson);

        groupcast.setFilter(filterJson);
        groupcast.setTicker("Android groupcast ticker");
        groupcast.setTitle("中文的title");
        groupcast.setText("Android groupcast text");
        groupcast.goAppAfterOpen();
        groupcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        groupcast.setChannelActivity("your channel activity");
        // TODO Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        groupcast.setProductionMode();
        //厂商通道相关参数
        groupcast.setChannelActivity("your channel activity");
        groupcast.setChannelProperties("abc");
        client.send(groupcast);
    }

    public void sendAndroidCustomizedcast(String appkey, String appMasterSecret) throws Exception {
        AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey, appMasterSecret);
        // TODO Set your alias here, and use comma to split them if there are multiple alias.
        // And if you have many alias, you can also upload a file containing these alias, then
        // use file_id to send customized notification.
        customizedcast.setAlias("alias", "alias_type");
        customizedcast.setTicker("Android customizedcast ticker");
        customizedcast.setTitle("中文的title");
        customizedcast.setText("Android customizedcast text");
        customizedcast.goAppAfterOpen();
        customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        // TODO Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        customizedcast.setProductionMode();
        //厂商通道相关参数
        customizedcast.setChannelActivity("your channel activity");
        customizedcast.setChannelProperties("abc");
        client.send(customizedcast);
    }

    public void sendAndroidCustomizedcastFile(String appkey, String appMasterSecret) throws Exception {
        AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey, appMasterSecret);
        // TODO Set your alias here, and use comma to split them if there are multiple alias.
        // And if you have many alias, you can also upload a file containing these alias, then
        // use file_id to send customized notification.
        String fileId = client.uploadContents(appkey, appMasterSecret, "aa" + "\n" + "bb" + "\n" + "alias");
        customizedcast.setFileId(fileId, "alias_type");
        customizedcast.setTicker("Android customizedcast ticker");
        customizedcast.setTitle("中文的title");
        customizedcast.setText("Android customizedcast text");
        customizedcast.goAppAfterOpen();
        customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        // TODO Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        customizedcast.setProductionMode();
        //厂商通道相关参数
        customizedcast.setChannelActivity("your channel activity");
        customizedcast.setChannelProperties("abc");
        client.send(customizedcast);
    }

    public void sendAndroidFilecast(String appkey, String appMasterSecret) throws Exception {
        AndroidFilecast filecast = new AndroidFilecast(appkey, appMasterSecret);
        // TODO upload your device tokens, and use '\n' to split them if there are multiple tokens
        String fileId = client.uploadContents(appkey, appMasterSecret, "aa" + "\n" + "bb");
        filecast.setFileId(fileId);
        filecast.setTicker("Android filecast ticker");
        filecast.setTitle("中文的title");
        filecast.setText("Android filecast text");
        filecast.goAppAfterOpen();
        filecast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        //厂商通道相关参数
        filecast.setChannelActivity("your channel activity");
        filecast.setChannelProperties("abc");
        client.send(filecast);
    }



    public void sendIOSGroupcast(String appkey, String appMasterSecret) throws Exception {
        IOSGroupcast groupcast = new IOSGroupcast(appkey, appMasterSecret);
        /*  TODO
         *  Construct the filter condition:
         *  "where":
         *	{
         *		"and":
         *		[
         *			{"tag":"iostest"}
         *		]
         *	}
         */
        JSONObject filterJson = new JSONObject();
        JSONObject whereJson = new JSONObject();
        JSONArray tagArray = new JSONArray();
        JSONObject testTag = new JSONObject();
        testTag.put("tag", "iostest");
        tagArray.put(testTag);
        whereJson.put("and", tagArray);
        filterJson.put("where", whereJson);
        System.out.println(filterJson.toString());

        // Set filter condition into rootJson
        groupcast.setFilter(filterJson);
        //groupcast.setAlert("IOS 组播测试");
        //alert的值设置为字典
        groupcast.setAlert("今日天气", "subtitle", "今日可能下雨🌂");
        groupcast.setBadge(0);
        groupcast.setSound("default");
        // TODO set 'production_mode' to 'true' if your app is under production mode
        groupcast.setTestMode();
        client.send(groupcast);
    }

    public void sendIOSCustomizedcast(String appkey, String appMasterSecret) throws Exception {
        IOSCustomizedcast customizedcast = new IOSCustomizedcast(appkey, appMasterSecret);
        // TODO Set your alias and alias_type here, and use comma to split them if there are multiple alias.
        // And if you have many alias, you can also upload a file containing these alias, then
        // use file_id to send customized notification.
        customizedcast.setAlias("alias", "alias_type");
        //customizedcast.setAlert("IOS 个性化测试");
        //alert的值设置为字典
        customizedcast.setAlert("今日天气", "", "今日可能下雨🌂");
        customizedcast.setBadge(0);
        customizedcast.setSound("default");
        // TODO set 'production_mode' to 'true' if your app is under production mode
        customizedcast.setTestMode();
        client.send(customizedcast);
    }

    public void sendIOSFilecast(String appkey, String appMasterSecret) throws Exception {
        IOSFilecast filecast = new IOSFilecast(appkey, appMasterSecret);
        // TODO upload your device tokens, and use '\n' to split them if there are multiple tokens
        String fileId = client.uploadContents(appkey, appMasterSecret, "aa" + "\n" + "bb");
        filecast.setFileId(fileId);
        //filecast.setAlert("IOS 文件播测试");
        //alert的值设置为字典
        filecast.setAlert("今日天气", "", "今日可能下雨🌂");
        filecast.setBadge(0);
        filecast.setSound("default");
        // TODO set 'production_mode' to 'true' if your app is under production mode
        filecast.setTestMode();
        client.send(filecast);
    }


}
